/*
 * @Date: 2022-03-02 17:43:19
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc:
 * @LastEditTime: 2022-03-22 10:02:13
 * @FilePath: \server\system\controller\autocode\autocode.go
 */
package autocode

import (
	"go2admin_pure/system/core/request"
	"go2admin_pure/system/core/response"
	"go2admin_pure/system/model/Autocode"
	"go2admin_pure/system/model/RequestModel"
	"go2admin_pure/system/service/BaseService"
	"github.com/gin-gonic/gin"
)

var AutocodeService BaseService.AutocodeService

func Create(c *gin.Context) {
	var r RequestModel.CodeCreate
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}
	if err := AutocodeService.Create(r); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.OkWithData("", c)
	}
}

func Remove(c *gin.Context) {
	var r RequestModel.CodeRemove
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}
	if err := AutocodeService.Remove(r.TableName); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.OkWithData("", c)
	}
}

func TableList(c *gin.Context) {
	response.OkWithData(AutocodeService.TableList(), c)
}

func ColumnList(c *gin.Context) {
	var r Autocode.TableModel
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}
	response.OkWithData(AutocodeService.TableFields(r.TableName), c)
}

func PermissionDir(c *gin.Context) {
	response.OkWithData(AutocodeService.PermissionDir(), c)
}
